package com.nervenets.web.controller.handler.admin.setting;


import com.nervenets.web.annotation.NeedValidation;
import com.nervenets.web.controller.handler.Handler;
import com.nervenets.web.controller.handler.common.EmptyParams;
import com.nervenets.web.redis.domain.global.GlobalRule;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by joe on 17-3-29.
 */
public class GlobalRuleGetHandler extends Handler<EmptyParams> {

    @Override
    public String handle(EmptyParams params, HttpServletRequest request) throws Exception {
        List<GlobalRuleFiled> ruleParams = new ArrayList<GlobalRuleFiled>();
        final Field[] fields = GlobalRuleParams.class.getDeclaredFields();
        for (Field field : fields) {
            final String fieldName = field.getName();
            String description = null;
            if (field.isAnnotationPresent(NeedValidation.class)) {
                final NeedValidation needValidation = field.getAnnotation(NeedValidation.class);
                description = needValidation.description();
            }
            final Class<?> fieldType = field.getType();
            final String fieldTypeName = fieldType.getSimpleName();
            ruleParams.add(new GlobalRuleFiled(fieldName, description, fieldTypeName));
        }
        return successMessage(new Model(globalVariablesService.getGlobalRule(), ruleParams));
    }

    @Override
    public String result() throws Exception {
        return null;
    }

    class Model {
        private GlobalRule rule;
        private List<GlobalRuleFiled> params = new ArrayList<>();

        public Model(GlobalRule rule, List<GlobalRuleFiled> params) {
            this.rule = rule;
            this.params = params;
        }
    }
}
